<!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8">
    <meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1">
    <title>编辑任务</title>
 <link rel="stylesheet" type="text/css" href="http://192.168.2.92/static/layui/css/layui.css">
  <script src="http://192.168.2.92/static/layui/layui.js"></script>
 <script src="http://192.168.2.92/static/js/jquery-1.11.3.min.js"></script>

    <script>
        function f() {
            check = false
            reg = "http"
            case_col = $("input[name='case_col']").val()
            yapi_url = $("input[name='yapi_url']").val()
            if (yapi_url !== "" && yapi_url.indexOf(reg) == 0 && case_col !== "") {
                return true
            } else {
                return false
            }
        }
    </script>
     
    <script type="text/html" id="barDemoCi">
                 <a id="deleteBtnCi" class="layui-btn layui-btn-danger layui-btn-xs" lay-event="del">删除</a>
           </script>

    <script>
        layui.use('table', function () {
            var table = layui.table;
            table.on('tool(cit)', function (obj) {
                var data = obj.data;
                var kk = data['id'];
                if (obj.event === 'del') {
                    layer.confirm('确认删除么', function (index) {
                        $.ajax({
                            url: "/ci/del_ci",
                            data: {id: kk},
                            type: "get",
                            success: function (data) {
                                table.reload('ci_table_c', {
                                    // url:'http://127.0.0.1/q_ci_on_task',
                                    // where: { "id": task_id },
                                });
                                layer.msg('别再点了，已经被删了', {time: 800});
                                return;
                            },
                            error: function () {
                            }
                        })
                    });
                }
            });
        });
    </script>


    <script>
        $(document).ready(function () {
            var task_id = $("input[name='task_id']").val()
            var id = location.href.split('id=')[1]
            function form1(id) {
                $.ajax({
                    type: "GET",
                    url: "/ci/selectonttask",
                    data: {"id": id},
                    dataType: "JSON",
                    success: function (result) {
                        $("input[name='task_id']").val(result["id"])
                        $("input[name='task_name']").val(result["task_name"])
                        $("input[name='job_name']").val(result["job_name"])
                        $("textarea[name='ding_token']").val(result["ding_token"])
                    }
                });
            }

            form1(id)

            $("#task_edit_submit").click(function () {
                var task_id_edit = $("input[name='task_id']").val()
                task_name = $("input[name='task_name']").val()
                job_name = $("input[name='job_name']").val()
                ding_token = $("textarea[name='ding_token']").val()
                $.ajax({
                    type: "POST",
                    url: "/ci/updatetask",
                    data: {
                        task_id: task_id_edit,
                        job_name: job_name,
                        ding_token: ding_token,
                        task_name: task_name
                    },
                    success: function (res) {
                        form1(task_id_edit)
                    },
                    error: function (xhr) {
                        layer.msg("网络出错，请稍等再试")
                    }

                });
            });

            layui.use('table', function () {
                var table = layui.table;
                table.render({
                    elem: '#ci_table_c',
                    id: "ci_table_c",
                    url: "/ci/q_ci_on_task",
                    page: true,
                    where: {"id": id},
                    cols: [[
                        {field: 'id', title: 'ID', width:80,sort: true, fixed: 'left'}
                        , {field: 'case_col',width:200, title: 'case_col'}
                        , {field: 'yapi_url', title: 'yapi_url'}
                        , {fixed: 'right', title: '操作',width:100, align: 'center', toolbar: '#barDemoCi'}
                    ]],
                    parseData: function (res) {
                        var result;
                        if (this.page.curr) {
                            result = res.data.slice(this.limit * (this.page.curr - 1), this.limit * this.page.curr);
                        } else {
                            result = res.data.slice(0, this.limit);
                        }
                        return {
                            "code": res.code, //解析接口状态
                            "msg": res.msg, //解析提示文本
                            "count": res.count, //解析数据长度
                            "data": result //解析数据列表
                        };
                    }
                });
            });

            function form2() {
                <!--    table ci 刷新-->
            }


            <!--  提交ci任务表单-->
            $("#ci_submit").click(function () {
                case_col = $("input[name='case_col']").val()
                yapi_url = $("input[name='yapi_url']").val()
                task_id_ = $("input[name='task_id']").val()

                if (f()) {
                    $.ajax({
                        type: "POST",
                        url: '/ci/add_ci',
                        data:
                            {
                                id: id,
                                case_col: case_col,
                                yapi_url: yapi_url
                            },
                        success: function () {
                            $("input[name='case_col']").val("")
                            $("input[name='yapi_url']").val("")

                            layui.use('table', function () {
                                var table = layui.table;
                                table.reload('ci_table_c', {
                                    url: '/ci/q_ci_on_task',
                                    where: {"id": task_id_},
                                });
                            });


                        },
                        error: function () {
                            layui.use('layer', function () {
                                var layer = layui.layer;
                                layer.msg('网络可能有点小问题哈', {time: 2000});
                            });
                        }
                    })
                } else {
                    layui.use('layer', function () {
                        var layer = layui.layer;
                        layer.msg('Yapi链接必须以http开头', {time: 2000});
                    });
                }
            })
        });
    </script>

    <style>
        .from-task {
            width: 90%;
            margin: 3% auto;

        }

        .ci-task {
            width: 90%;
            margin: 0% auto;
        }

        .ci_table {
            width: 90%;
            margin: 0% auto;
        }

    </style>
</head>
<body>
<div class="from-task">
    <form class="layui-form" id="hahah">
        <input type="text" name="task_id" id="task_id" required lay-verify="required" placeholder="请输入ID"
               autocomplete="off" class="layui-input" style="visibility: hidden;">
        <div class="layui-form-item">
            <label class="layui-form-label">任务名称</label>
            <div class="layui-input-block">
                <input type="text" name="task_name" required lay-verify="required" placeholder="请输入任务名称"
                       autocomplete="off" class="layui-input">
            </div>
        </div>
        <div class="layui-form-item">
            <label class="layui-form-label">工作名称</label>
            <div class="layui-input-block">
                <input type="text" name="job_name" required lay-verify="required" placeholder="请输入工作名称"
                       autocomplete="off" class="layui-input">
            </div>
        </div>

        <div class="layui-form-item layui-form-text">
            <label class="layui-form-label">Token</label>
            <div class="layui-input-block">
                <textarea name="ding_token" name="ding_token" placeholder="请输入ding_token"
                          class="layui-textarea"></textarea>
            </div>
        </div>
        <div class="layui-form-item">
            <div class="layui-input-block">
                <button class="layui-btn" id="task_edit_submit">保存</button>
            </div>
        </div>
    </form>
</div>

<div class="ci-task">
    <div class="layui-form-item">
        <label class="layui-form-label">测试集</label>
        <div class="layui-input-block">
            <input type="text" name="case_col" required lay-verify="required" placeholder="请输入case_col"
                   autocomplete="off" class="layui-input">
        </div>
    </div>
    <div class="layui-form-item">
        <label class="layui-form-label">Yapi链接</label>
        <div class="layui-input-block">
            <input type="text" name="yapi_url" required lay-verify="required" placeholder="请输入yapi_url"
                   autocomplete="off" class="layui-input">
        </div>
    </div>

    <div class="layui-form-item">
        <div class="layui-input-block">
            <button class="layui-btn" id="ci_submit">添加</button>
        </div>
    </div>

</div>

<div class="ci_table">
    <table id="ci_table_c" lay-filter="cit"></table>
</div>


</body>

</html>